package org.jeecg.modules.basedata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.netty.handler.timeout.ReadTimeoutException;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.entity.basedata.BaseCustomer;
import org.jeecg.entity.basedata.BaseRoom;
import org.jeecg.entity.basedata.RoomVo;
import org.jeecg.modules.basedata.mapper.BaseProjectMapper;
import org.jeecg.modules.basedata.mapper.BaseRoomMapper;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.model.SysDepartTreeModel;
import org.jeecg.modules.system.model.TreeModel;
import org.jeecg.modules.system.util.FindsDepartsChildrenUtil;
import org.jeecg.modules.basedata.service.IBaseCustomerService;
import org.jeecg.modules.basedata.service.IBaseRoomService;
import org.jeecg.vo.basedata.BaseRoomPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 房间
 * @Author: jeecg-boot
 * @Date:   2019-07-03
 * @Version: V1.0
 */
@Service
public class BaseRoomServiceImpl extends ServiceImpl<BaseRoomMapper, BaseRoom> implements IBaseRoomService {

	@Autowired
	private BaseRoomMapper baseRoomMapper;

	@Autowired
	private IBaseCustomerService customerService;

	@Autowired
	private BaseProjectMapper baseProjectMapper;

	@Override
	public List<SysDepartTreeModel> queryFloorTreeList() {
		LambdaQueryWrapper<SysDepart> query = new LambdaQueryWrapper<SysDepart>();
		query.eq(SysDepart::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
		query.eq(SysDepart::getDepartProperty, CommonConstant.DEPART_PROPERTY_COMPANY);
		query.orderByAsc(SysDepart::getDepartOrder);
		List<SysDepart> list = baseRoomMapper.queryFloorTreeList();
		// 调用wrapTreeDataToTreeList方法生成树状数据
		List<SysDepartTreeModel> listResult = FindsDepartsChildrenUtil.wrapTreeDataToTreeList(list,"floor");
		return listResult;
	}

	/**
	 * 查询某一楼栋下 已租/未租房屋信息
	 * @param floorId
	 * @param rentStatus
	 * @return
	 */
	@Override
	public List<BaseRoom> getByRentStatus(String floorId, Integer rentStatus) {
		return baseRoomMapper.getByRentStatus(floorId, rentStatus);
	}

	@Override
	public List<TreeModel> findRoomTreeList(BaseRoom baseRoom) {
		List<TreeModel> list = baseRoomMapper.findRoomTreeList(baseRoom);
		// 调用wrapTreeDataToTreeList方法生成树状数据
//		List<SysDepartTreeModel> listResult = FindsDepartsChildrenUtil.wrapTreeDataToTreeList(list,"floor");
		return list;
	}
	@Override
	public List<SysDepartTreeModel> queryRoomTreeList(BaseRoom baseRoom) {
		List<SysDepart> list = baseRoomMapper.queryRoomTreeList(baseRoom);
		List<SysDepartTreeModel> listResult = FindsDepartsChildrenUtil.wrapTreeDataToTreeList(list,"room",CommonConstant.DEPART_PROPERTY_OFFICE);
		return listResult;
	}

	@Override
	public BaseRoomPage selectByMainId(String mainId) {
		return baseRoomMapper.selectByMainId(mainId);
	}

	@Override
	public BaseRoom getBaseRoomByName(String roomName, String floorId) {
		return baseRoomMapper.getBaseRoomByName(roomName, floorId);
	}

    @Override
	public void updateBatchRoomStatus(List<BaseRoom> roomList) {
		baseRoomMapper.updateBatchRoomStatus(roomList);
	}

	@Override
	@Transactional
	public void addRoomCustomer(List<BaseRoom> roomList, List<BaseCustomer> customerList) {
		try {
			this.saveBatch(roomList);
			customerService.saveBatch(customerList);
		}catch (Exception e){
			log.error(e.getMessage(),e);
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		//baseRoomInfoMapper.deleteByMainId(id);
		baseRoomMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			//baseRoomInfoMapper.deleteByMainId(id.toString());
			baseRoomMapper.deleteById(id);
		}
	}

	@Override
	public void updateRoomStatus(String upRoomId, String upRoomStatus) {
	      baseRoomMapper.updateRoomStatus(upRoomId,upRoomStatus);
	}

	/**
	 * 迁出时同步房间状态
	 *
	 * @param syRoomId
	 */
	@Override
	public void syupdateRoomStatus(String syRoomId) {
		baseRoomMapper.syupdateRoomStatus(syRoomId);
	}

	@Override
	public List<BaseRoom> getByRoomIds(String [] roomIds){
		List<BaseRoom> list = baseRoomMapper.getByRoomIds(roomIds);
		return list;
	}
	@Override
	public BaseRoom getByBaseRoom(String id){
		 BaseRoom  room = baseRoomMapper.getByBaseRoom(id);
		 return room;
	}

	@Override
	public Page<BaseRoom> listByParams(Page<BaseRoom> page,BaseRoom baseRoom) {
		return page.setRecords(baseRoomMapper.listByParams(page,baseRoom));
	}

	@Override
	public Integer getPeopeleCount(String id) {
		return baseRoomMapper.getPeopeleCount(id);
	}

	@Override
	public Integer getPeopeleCounts(String[] id) {
		return baseRoomMapper.getPeopeleCounts(id);
	}

	@Override
	public List<RoomVo> getByFloorId(String floorId){
		return baseRoomMapper.getByFloorId(floorId);
	}

	@Override
	public String queryRoomName(String floorId, String roomName) {
		return baseRoomMapper.queryRoomName(floorId,roomName);
	}

	@Override
	public BaseRoom getByFloorIdName(String floorId, String name){
		return baseRoomMapper.getByFloorIdName(floorId,name);
	}

	@Override
	public Integer checkRoomName(String name, String floorId) {
		return baseRoomMapper.checkRoomName(name,floorId);
	}

	@Override
	public List<SysDepartTreeModel> queryTenantList(String floorId) {

		 return baseProjectMapper.queryTenantList(floorId);


	}
	@Override
	public List<SysDepartTreeModel> queryCustomerList(String floorId) {
		return baseProjectMapper.queryCustomerList(floorId);
	}

	@Override
	public String getRoomAddress(String id) {
		return baseMapper.getRoomAddress(id);
	}

	@Override
	public List<String> getAllFeeItemId(List<String> roomId) {
		return baseMapper.getAllFeeItemId(roomId);
	}

	@Override
	public void updateSpiltDate(BaseRoom baseRoom) {
		baseMapper.updateSpiltDate(baseRoom);
	}

	@Override
	public Page<BaseRoom> queryRoomFeeList(Page<BaseRoom> pageList, BaseRoom baseRoom) {
		return baseMapper.queryRoomFeeList(pageList,baseRoom);
	}

	@Override
	public Page<BaseRoom> getRoomList(Page<BaseRoom> pageList, String projectId,String feeRuleId,String roomId,String floorId) {
		return baseMapper.getRoomList(pageList,projectId,feeRuleId,roomId,floorId);
	}

	/**
	 * 更新房间名
	 * @param roomist
	 */
	@Override
	public void updateBatchRoomName(List<BaseRoom> roomist,BaseRoom baseRoom) {
		baseRoomMapper.updateBatchRoomName(roomist,baseRoom);
	}


}
